<?php
/**
 * @title 1410. HTML 实体解析器
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param String $text
     * @return String
     */
    function entityParser($text) {
        /**
         * @since 2023-11-23 长度
         */
        $len = strlen($text);

        /**
         * @since 2023-11-23 解码后字符串
         */
        $decodeString = "";

        /**
         * @since 2023-11-23 解析
         */
        for($i=0; $i<$len; $i++){
            /**
             * @since 2023-11-23 添加的字符
             */
            $addChar = $text[$i];

            /**
             * @since 2023-11-23 判断是否特殊字符开头 &
             */
            if($addChar == "&") {
                /**
                 * @since 2023-11-23 下标i字符之后，还有多少个字符
                 */
                $remainCharLen = $len - 1 - $i;

                /**
                 * @since 2023-11-23 大于号 &gt; >
                 */
                if ($remainCharLen>=3 AND $text[$i + 1] == "g" and $text[$i + 2] == "t" and $text[$i + 3] == ";") {
                    $addChar = ">";
                    $i += 3;
                } elseif ($remainCharLen>=3 AND $text[$i + 1] == "l" and $text[$i + 2] == "t" and $text[$i + 3] == ";") {
                    /**
                     * @since 2023-11-27 小于号 &lt; <
                     */
                    $addChar = "<";
                    $i += 3;
                } elseif ($remainCharLen >= 4 AND $text[$i + 1] == "a" and $text[$i + 2] == "m" and $text[$i + 3] == "p" and $text[$i + 4] == ";") {
                    /**
                     * @since 2023-11-23 连接符 &amp; &
                     */
                    $addChar = "&";
                    $i += 4;
                } elseif ($remainCharLen >= 5 AND $text[$i + 1] == "q" and $text[$i + 2] == "u" and $text[$i + 3] == "o" and $text[$i + 4] == "t" and $text[$i + 5] == ";") {
                    /**
                     * @since 2023-11-23 双引号 &quot; "
                     */
                    $addChar = "\"";
                    $i += 5;
                } elseif ($remainCharLen >= 5 AND $text[$i + 1] == "a" and $text[$i + 2] == "p" and $text[$i + 3] == "o" and $text[$i + 4] == "s" and $text[$i + 5] == ";") {
                    /**
                     * @since 2023-11-23 单引号 &apos; '
                     */
                    $addChar = "'";
                    $i += 5;
                } elseif ($remainCharLen >= 6 AND $text[$i + 1] == "f" and $text[$i + 2] == "r" and $text[$i + 3] == "a" and $text[$i + 4] == "s" and $text[$i + 5] == "l" and $text[$i + 6] == ";") {
                    /**
                     * @since 2023-11-23 斜杠 &frasl; /
                     */
                    $addChar = "/";
                    $i += 6;
                } else {}
            } else {}

            /**
             * @since 2023-11-23 添加字符
             */
            $decodeString .= $addChar;
            // echo "i={$i}, char=", $addChar, PHP_EOL;
        }

        /**
         * @return
         */
        return $decodeString;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.23_2.php
 */
$datas = [
    ["&amp; is an HTML entity but &ambassador; is not."],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->entityParser(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
